/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.orkut.client.api.samples.greetings.client.DataModel;
import com.google.orkut.client.api.samples.greetings.client.GreetingsMessages;
import com.google.orkut.client.api.samples.greetings.client.DataModel.DataModelUpdateCallback;
import com.google.orkut.client.api.samples.greetings.client.services.AlbumInfo;
import com.google.orkut.client.api.samples.greetings.client.services.PhotoInfo;
import com.google.orkut.client.api.samples.greetings.client.services.UserInfo;
import com.google.orkut.client.api.samples.greetings.client.ui.AlbumSelector.AlbumSelectedCallback;
import com.google.orkut.client.api.samples.greetings.client.ui.PhotoSelector.PhotoSelectedCallback;
import com.google.orkut.client.api.samples.greetings.client.ui.ScrapSentDialogBox.ScrapSendDialogBoxCallback;

import java.util.ArrayList;

/**
 * The Main UI page.
 *
 * @author Shishir Birmiwal
 */
public class MainPage extends Composite
implements AlbumSelectedCallback, PhotoSelectedCallback,
    DataModelUpdateCallback, ScrapSendDialogBoxCallback {
  private static MainPageUiBinder uiBinder = GWT.create(MainPageUiBinder.class);

  interface MainPageUiBinder extends UiBinder<Widget, MainPage> {
  }

  @UiField
  HTML title;

  @UiField(provided = true)
  AlbumSelector albumSelector;

  @UiField(provided = true)
  PhotoSelector photoSelector;

  @UiField(provided = true)
  FriendSelector friendSelector;

  @UiField(provided = true)
  UserInfoWidget userInfo;

  @UiField(provided = true)
  ScrapModule scrapModule;

  private final GreetingsMessages msgs;

  private final DataModel dataModel;

  @Inject
  public MainPage(
      AlbumSelector albumSelector,
      DataModel dataModel,
      GreetingsMessages msgs,
      PhotoSelector photoSelector,
      UserInfoWidget userInfoWidget,
      FriendSelector friendSelector,
      ScrapModule scrapModule) {
    this.albumSelector = albumSelector;
    this.dataModel = dataModel;
    this.msgs = msgs;
    this.photoSelector = photoSelector;
    userInfo = userInfoWidget;
    this.friendSelector = friendSelector;
    this.scrapModule = scrapModule;
    dataModel.registerCallback(this);
    albumSelector.setAlbumSelectedCallback(this);
    photoSelector.setPhotoSelectedCallback(this);
    scrapModule.setOnScrapSuccessCallback(this);
    initWidget(uiBinder.createAndBindUi(this));
    title.setHTML(msgs.mainpageTitleHtml());
  }

  @Override
  public void albumSelected(AlbumInfo album) {
    albumSelector.collapse();
    photoSelector.setVisible(true);
    photoSelector.expand();
    dataModel.updateSelectedAlbum(album);
  }

  @Override
  public void photoSelected(PhotoInfo photo) {
    photoSelector.collapse();
    friendSelector.expand();
    friendSelector.setVisible(true);
    scrapModule.setSelectedPhoto(photo);
  }

  @Override
  public void update(DataModel data, Type type) {
    if (Type.USER_INFO_TABLE.equals(type)) {
      ArrayList<UserInfo> users = new ArrayList<UserInfo>();
      for (UserInfo user : data.userInfo.values()) {
        if (user.id.equals(data.selectedUser.id)) {
          continue;
        }
        users.add(user);
      }
    }
  }

  @Override
  public void sendAgain() {
    friendSelector.reset();
    friendSelector.expand();
    scrapModule.setVisible(false);
  }

  @Override
  public void startOver() {
    friendSelector.reset();
    friendSelector.setVisible(false);
    photoSelector.setVisible(false);
    scrapModule.setVisible(false);
    albumSelector.expand();
  }
}
